/**
 * Created by python on 18-12-15.
 */
/**
 * Created by python on 18-12-14.
 */
/**
 * Created by python on 18-12-10.
 */
/**
 * Created by python on 18-12-10.
 */


/**
 * Created by python on 18-12-10.
 */
var vm = new Vue({
    el: '#app',
    data: {
        delimiters: ['{{', '}}'],
        host: HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        questions: '',

        nickname: '',
        mobile: '',
        city: '',
        school: '',
        avatar: '',
        personal_web: '',
        profile: '',
        company: '',
        birth_time: '',
        setcity: true,
        setschool: true,
        setcompany: true,
        setweb: true,
        setprofile: true,
        flag:1,
        answers:'',
        histories: []
    },
    mounted: function () {
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.host + '/user/', {
                // 向后端传递JWT token的方法
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                // withCredentials: true,
                responseType: 'json',
            })
                .then(response => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.nickname = response.data.nickname;
                    this.mobile = response.data.mobile;
                    this.birth_time = response.data.birth_time;
                    this.city = response.data.city;
                    this.school = response.data.school;
                    this.personal_web = response.data.personal_web;
                    this.profile = response.data.profile;
                    this.avatar = response.data.avatar;
                    this.company = response.data.company;
                    this.questions=response.data.questioner;
                    this.answers=response.data.answer;
                    alert(this.answers)

                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/person-loginsign.html?next=/person-homepage.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-homepage.html';
        }


    }

    ,
    methods: {
        // 修改城市，保存城市


        get_answer:function () {

             if (this.user_id && this.token) {
            axios.get(this.host + '/user/answer/', {
                // 向后端传递JWT token的方法
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                // withCredentials: true,
                responseType: 'json',
            })
                .then(response => {
                    // 加载用户数据

                    this.answers=response.data.answer;

                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/person-loginsign.html?next=/person-homepage.html';
                    }
                });
        }




        }
     ,
        change:function (a) {
            this.flag=a;

        },
        set_city: function () {
            //
            axios.patch(this.host + '/user/',
                {city: this.city},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.setcity = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        },
        set_school: function () {
            // 以后设置一个城市选项
            axios.patch(this.host + '/user/',
                {school: this.school},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.setschool = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        set_company: function () {
            // 以后设置一个城市选项
            axios.patch(this.host + '/user/',
                {company: this.company},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.setcompany = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        set_web: function () {
            // 以后设置一个城市选项
            axios.patch(this.host + '/user/',
                {personal_web: this.personal_web},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.setweb = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
        ,
        set_profile: function () {
            // 以后设置一个城市选项
            axios.patch(this.host + '/user/',
                {profile: this.profile},
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.setprofile = true;

                })
                .catch(error => {
                    alert(error.data);
                });
        }
    },

});

